class Cookie {
    set(key, val, time = 24) {   //time(单位：小时)
        let date = new Date();
        let expiresHours = time ? time : 1;
        date.setTime(date.getTime() + expiresHours * 3600 * 1000); //格式化为cookie识别的时间
        document.cookie = key + "=" + val + ";expires=" + date.toGMTString();  //设置cookie
    }
    get(key) {
        let cookie = document.cookie.replace(/[ ]/g, "");  //获取cookie，并且将获得的cookie格式化，去掉空格字符
        let arrCookie = cookie.split(";")  //将获得的cookie以"分号"为标识 将cookie保存到arrCookie的数组中
        let val = null;
        // console.log(arrCookie,key)
        for (let i = 0; i < arrCookie.length; i++) {
            let arr = arrCookie[i].split("=");
            if (key == arr[0]) {
                val = arr[1];
                break;
            }
        }
        return val;
    }
    delete(key) {
        let date = new Date(); //获取当前时间
        date.setTime(date.getTime() - 1000); //将date设置为过去的时间
        document.cookie = key + "=0; expires =" + date.toGMTString();//设置cookie
    }
    clear() {
        let keys = document.cookie.match(/[^ =;]+(?=\=)/g);
        if (keys) {
            for (let i = keys.length; i--;)
                document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString();
        }
    }
}
//防抖
//使用举例：window.addEventListener('scroll', debounce(()=>console.log("防抖测试"), 1000));
function debounce(func, delay = 1000) {
    let timer;
    return function () {
        let that = this;
        let args = arguments;
        if (timer) {
            clearTimeout(timer)
        };
        timer = setTimeout(() => {
            func.apply(that, args)
        }, delay);
    }
}
//节流
//使用举例： window.addEventListener('scroll', throttle(()=>{console.log("节流测试")}, 1000));
function throttle(func, delay = 1000) {
    let timer = null;
    return function () {
        let that = this;
        let args = arguments;
        if (!timer) {
            timer = setTimeout(function () {
                func.apply(that, args);
                timer = null;
            }, delay);
        }
    }
}
function getDHmsDiffByMs(ms, type = "s") {    //type  精确到什么位置
    let d = Math.floor(ms / (24 * 3600 * 1000));
    if (type == "d") return [d];
    let msD = ms % (24 * 3600 * 1000);
    let h = Math.floor(msD / (3600 * 1000));
    if (type == "h") return [d, h];
    let msH = msD % (3600 * 1000);
    let m = Math.floor(msH / (60 * 1000));
    if (type == "m") return [d, h, m];
    let msM = msH % (60 * 1000);
    let s = Math.floor(msM / 1000);
    return [d, h, m, s]
}
//获取时间差，用于前端灵活显示用
function getDateDiffByText(time) {
    let nowMs = new Date().getTime();
    let oldMs = new Date(time).getTime();
    let absMs = nowMs - oldMs;
    let dhms = getDHmsDiffByMs(absMs);
    let text = "";
    switch (true) {
        case dhms[0] == 0 && dhms[1] == 0 && dhms[2] == 0: text = "刚刚"; break;
        case dhms[0] == 0 && dhms[1] == 0: text = dhms[2] + "分钟前"; break;
        case dhms[0] == 0: text = dhms[1] + "小时前"; break;
        case dhms[0] < 30: text = dhms[0] + "天前"; break;
        case Math.floor(dhms[0] / 30.5) < 12: text = Math.floor(dhms[0] / 30.5) + "个月前"; break;
        default: text = Math.floor(dhms[0] / 365.25) + "年前"; break;
    }
    return text;
}
export {
    Cookie, debounce, throttle,getDHmsDiffByMs,getDateDiffByText
}